package com.wangsh.userscenter.back.controller;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.wangsh.userscenter.common.controller.BaseController;
import com.wangsh.userscenter.common.util.EncryptUtil;
import com.wangsh.userscenter.common.util.PageInfoUtil;
import com.wangsh.userscenter.service.users.api.IUsersService;
import com.wangsh.userscenter.service.users.pojo.AAdmins;

/**
 * 管理员登陆后的主页面
 * @author wangsh_sxt
 *
 */
@Controller
@RequestMapping("/back/admins/")
public class AdminsBackController extends BaseController
{
	@Autowired
	private IUsersService usersService;
	
	/**
	 * 管理员首页
	 * @return
	 */
	@RequestMapping("/main.html")
	public String main()
	{
		return "/back/main";
	}
	
	/**
	 * 管理员首页
	 * @return
	 */
	@RequestMapping("/leftMenu.html")
	public String leftMenu()
	{
		return "/back/leftMenu";
	}
	
	/**
	 * 管理员列表
	 * @return
	 */
	@RequestMapping("/adminsList.html")
	public String adminsList(HttpServletRequest request)
	{
		/* 接收参数 */
		String keyword = request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");
		
		if(keyword == null)
		{
			keyword = "" ; 
		}
		keyword = keyword.trim();
		if(status == null)
		{
			status = "" ; 
		}
		if(st == null)
		{
			st = "" ; 
		}
		if(ed == null)
		{
			ed = "" ; 
		}
		
		Date stDate = null ; 
		Date edDate = null ; 
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			stDate = this.dateUtil.parseDateTime(st);
			edDate = this.dateUtil.parseDateTime(ed);
		}
		
		/* 查询数据 */
		PageInfoUtil pageInfoUtil = this.setPageInfoCond(request);
		Map<String, Object> condMap = new HashMap<String,Object>();
		condMap.put("keyword", keyword);
		condMap.put("status", status);
		condMap.put("st", stDate);
		condMap.put("ed", edDate);
		List<AAdmins> adminsList = this.usersService.findCondListAdminsService(pageInfoUtil, condMap);
		
		/* 存储结果 */
		request.setAttribute("adminsList", adminsList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		return "/back/users/adminsList";
	}
	
	/**
	 * 管理员添加
	 * @return
	 */
	@RequestMapping("/adminsInsert.html")
	public String adminsInsert()
	{
		return "/back/users/adminsInsert";
	}
	
	/**
	 * 管理员添加提交
	 * @return
	 */
	@RequestMapping("/adminsInsertSubmit.html")
	public String adminsInsertSubmit(HttpServletRequest request , HttpServletResponse response ,
			AAdmins admins)
	{
		admins.setPassword(EncryptUtil.encode(admins.getPassword()));
		admins.setCreateTime(new Date());
		admins.setUpdateTime(new Date());
		admins.setLastLoginTime(new Date());
		JSONObject dbJSON = this.usersService.saveOneAdminsService(admins);
		if(dbJSON != null)
		{
			info = dbJSON.getString("info") + "" ; 
		}
		
		JSONObject resultJSON = this.returnJSON(request, info);
		this.printOut(request, response, resultJSON + "");
		return null;
	}
	
	/**
	 * 管理员更新
	 * @return
	 */
	@RequestMapping("/adminsUpdate.html")
	public String adminsUpdate(HttpServletRequest request)
	{
		String aid = request.getParameter("aid");
		Map<String, Object> condMap = new HashMap<String,Object>();
		condMap.put("id", aid);
		AAdmins admins = this.usersService.findOneAdminsService(condMap) ; 
		request.setAttribute("admins", admins);
		
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType)
			|| "updatePass".equalsIgnoreCase(operType)
			|| "resetPass".equalsIgnoreCase(operType))
		{
			return "/back/users/adminsUpdate";
		}
		return "/back/users/adminsInfo";
	}
	
	/**
	 * 管理员更新提交
	 * @return
	 */
	@RequestMapping("/adminsUpdateSubmit.html")
	public String adminsUpdateSubmit(HttpServletRequest request,HttpServletResponse response)
	{
		String aid = request.getParameter("aid");
		Map<String, Object> condMap = new HashMap<String,Object>();
		condMap.put("id", aid);
		AAdmins admins = this.usersService.findOneAdminsService(condMap) ; 
		
		boolean updateFlag = false ;
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			String email = request.getParameter("email");
			String trueName = request.getParameter("trueName");
			String qq = request.getParameter("qq");
			String phone = request.getParameter("phone");
			String status = request.getParameter("status");
			
			admins.setEmail(email);
			admins.setTrueName(trueName);
			admins.setQq(qq);
			admins.setPhone(phone);
			admins.setStatus(Byte.valueOf(status));
			updateFlag = true ;
		}else if("updatePass".equalsIgnoreCase(operType))
		{
			/* 处理修改密码和重置密码 */
			String souPass = request.getParameter("souPass");
			if(!EncryptUtil.checkPassword(souPass, admins.getPassword()))
			{
				this.info = "原密码不正确" ; 
				updateFlag = false ;
			}else
			{
				/* 新密码 */
				String password = request.getParameter("password");
				admins.setPassword(EncryptUtil.encode(password));
				updateFlag = true ;
			}
		}else if("resetPass".equalsIgnoreCase(operType))
		{
			/* 新密码 */
			String password = request.getParameter("password");
			admins.setPassword(EncryptUtil.encode(password));
			updateFlag = true ;
		}
		
		if(updateFlag)
		{
			admins.setUpdateTime(new Date());
			JSONObject dbJSON = this.usersService.updateOneAdminsService(admins);
			if(dbJSON != null)
			{
				info = dbJSON.getString("info") + "" ; 
			}
		}
		JSONObject resultJSON = this.returnJSON(request, info);
		this.printOut(request, response, resultJSON + "");
		return null;
	}
	
	/**
	 * 退出操作
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/logout.html")
	public String logout(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		info = "成功退出" ; 
		HttpSession session = request.getSession();
		session.removeAttribute("admins");
		
		/* 接收页面参数 */
		String returnUrl = request.getParameter("returnUrl");
		if(returnUrl == null)
		{
			returnUrl = "";
		}
		if(!"".equalsIgnoreCase(returnUrl))
		{
			returnUrl = URLDecoder.decode(returnUrl, "UTF-8");
			response.sendRedirect(returnUrl);
			return null ; 
		}
		
		request.setAttribute("info", info);
		return "/login";
	}
}
